package com.canaan.business.service.inOutStock.impl;

import java.util.Date;
import java.util.List;
import com.canaan.common.utils.DateUtils;
import com.canaan.common.utils.SecurityUtils;
import com.canaan.common.utils.TableNoUtil;
import com.canaan.common.utils.uuid.UUID;
import com.canaan.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.canaan.business.mapper.inOutStock.ReturnStockBillDetailMapper;
import com.canaan.business.domain.inOutStock.ReturnStockBillDetail;
import com.canaan.business.service.inOutStock.IReturnStockBillDetailService;
import com.canaan.common.annotation.TableName;
/**
 * 回库单明细Service业务层处理
 *
 * @author canaan
 * @date 2025-04-02
 */
@Service
public class ReturnStockBillDetailServiceImpl implements IReturnStockBillDetailService {
    @Resource
    private ReturnStockBillDetailMapper returnStockBillDetailMapper;

    /**
     * 查询回库单明细
     *
     * @param id 回库单明细主键
     * @return 回库单明细
     */
    @Override
    public ReturnStockBillDetail selectReturnStockBillDetailById(Long id) {
        return returnStockBillDetailMapper.selectReturnStockBillDetailById(id);
    }

    /**
     * 查询回库单明细列表
     *
     * @param returnStockBillDetail 回库单明细
     * @return 回库单明细
     */
    @Override
    public List<ReturnStockBillDetail> selectReturnStockBillDetailList(ReturnStockBillDetail returnStockBillDetail) {
        return returnStockBillDetailMapper.selectReturnStockBillDetailList(returnStockBillDetail);
    }

    /**
     * 新增回库单明细
     *
     * @param returnStockBillDetail 回库单明细
     * @return 结果
     */
    @Override
    public int insertReturnStockBillDetail(ReturnStockBillDetail returnStockBillDetail) {
        if(StringUtils.isBlank(returnStockBillDetail.getNo())){
            returnStockBillDetail.setNo(TableNoUtil.getNo(ReturnStockBillDetail.class));
        }
        returnStockBillDetail.setCreateBy(SecurityUtils.getUsername());
        returnStockBillDetail.setCreateTime(DateUtils.getNowDate());
        return returnStockBillDetailMapper.insertReturnStockBillDetail(returnStockBillDetail);
    }

    /**
     * 批量新增回库单明细
     *
     * @param returnStockBillDetailList 回库单明细列表
     * @return 结果
     */
    @Override
    public int batchInsertReturnStockBillDetail(List<ReturnStockBillDetail> returnStockBillDetailList) {
        String username = SecurityUtils.getUsername();
        Date currerTime = DateUtils.getNowDate();
        for (ReturnStockBillDetail returnStockBillDetail : returnStockBillDetailList){
            if(StringUtils.isBlank(returnStockBillDetail.getNo())){
                returnStockBillDetail.setNo(TableNoUtil.getNo(ReturnStockBillDetail.class));
            }
            returnStockBillDetail.setCreateBy(username);
            returnStockBillDetail.setCreateTime(currerTime);
        }
        int insertNum = 0;
        for (int i=0; i<returnStockBillDetailList.size();) {
            int endIndex = i+500;
            if (endIndex > returnStockBillDetailList.size()) {
                endIndex = returnStockBillDetailList.size();
            }
            insertNum = insertNum + returnStockBillDetailMapper.batchInsertReturnStockBillDetail(returnStockBillDetailList.subList(i, endIndex));
            i = endIndex;
        }
        return insertNum;
    }

    /**
     * 修改回库单明细
     *
     * @param returnStockBillDetail 回库单明细
     * @return 结果
     */
    @Override
    public int updateReturnStockBillDetail(ReturnStockBillDetail returnStockBillDetail) {
        returnStockBillDetail.setUpdateBy(SecurityUtils.getUsername());
        returnStockBillDetail.setUpdateTime(DateUtils.getNowDate());
        return returnStockBillDetailMapper.updateReturnStockBillDetail(returnStockBillDetail);
    }

    /**
     * 批量修改回库单明细
     *
     * @param returnStockBillDetailList 回库单明细列表
     * @return 结果
     */
    @Override
    public int batchUpdateReturnStockBillDetail(List<ReturnStockBillDetail> returnStockBillDetailList) {
        String username = SecurityUtils.getUsername();
        Date currerTime = DateUtils.getNowDate();
        for (ReturnStockBillDetail returnStockBillDetail : returnStockBillDetailList){
            returnStockBillDetail.setUpdateBy(username);
            returnStockBillDetail.setUpdateTime(currerTime);
        }
        int updateNum = 0;
        for (int i=0; i<returnStockBillDetailList.size();) {
            int endIndex = i+500;
            if (endIndex > returnStockBillDetailList.size()) {
                endIndex = returnStockBillDetailList.size();
            }
            updateNum = updateNum + returnStockBillDetailMapper.batchUpdateReturnStockBillDetail(returnStockBillDetailList.subList(i, endIndex));
            i = endIndex;
        }
        return updateNum;
    }

    /**
     * 批量删除回库单明细
     *
     * @param ids 需要删除的回库单明细主键集合
     * @return 结果
     */
    @Override
    public int deleteReturnStockBillDetailByIds(Long[] ids) {
        return returnStockBillDetailMapper.deleteReturnStockBillDetailByIds(ids);
    }

    /**
     * 删除回库单明细信息
     *
     * @param id 回库单明细主键
     * @return 结果
     */
    @Override
    public int deleteReturnStockBillDetailById(Long id) {
        return returnStockBillDetailMapper.deleteReturnStockBillDetailById(id);
    }

    /**
     * 查询回库单明细
     *
     * @param no 编号
     * @return 回库单明细
     */
    @Override
    public ReturnStockBillDetail selectReturnStockBillDetailByNo(String no) {
        return returnStockBillDetailMapper.selectReturnStockBillDetailByNo(no);
    }

    /**
     * 批量删除回库单明细
     *
     * @param nos 需要删除的编号集合
     * @return 结果
     */
    @Override
    public int deleteReturnStockBillDetailByNos(String[] nos) {
        return returnStockBillDetailMapper.deleteReturnStockBillDetailByNos(nos);
    }

    /**
     * 删除回库单明细信息
     *
     * @param no 编号
     * @return 结果
     */
    @Override
    public int deleteReturnStockBillDetailByNo(String no) {
        return returnStockBillDetailMapper.deleteReturnStockBillDetailByNo(no);
    }
}
